![]() |
PATH![]() |
![]() ![]() |
You should be familiar with the information in Script Objects before you read this section.
Figure 8-2 summarizes the scope of properties and variables declared at the top level of a script object. Sample scripts using each form of declaration follow.
Figure 8-2 Scope of property and variable declarations at the top level of a script object
The scope of a property declaration at the top level of a script object extends to any subsequent statements in that script object. Here's an example.
script Joe
property currentCount : 0
on increment()
set currentCount to currentCount + 1
return currentCount
end increment
end script
tell Joe to increment() --result: 1
tell Joe to increment() --result: 2
When it encounters the identifier currentCount at any level of the script object Joe , AppleScript associates it with the same identifier declared at the top level of the script object. The value of the property currentCount persists until you reinitialize the script object by running the script again.
The scope of a property declaration at the top level of a script object doesn't extend beyond the script object. Thus, it is possible to use the same identifier in different parts of a script to refer to different properties, as this example demonstrates:.
property currentCount : 0
script Joe
property currentCount : 0
on increment()
set currentCount to currentCount + 1
return currentCount
end increment
end script
tell Joe to increment() --result: 1
tell Joe to increment() --result: 2
currentCount --result: 0
AppleScript keeps track of the property currentCount declared at the top level of the script separately from the property currentCount declared within the script object Joe . Thus, the currentCount property declared at the top level of the script Joe is increased by 1 each time Joe is told to increment, but the currentCount property declared at the top level of the script is not affected.
Like the scope of a property declaration, the scope of a global variable declaration at the top level of a script object extends to any subsequent statements in that script object. However, as the next example demonstrates, AppleScript also associates a global variable with the same variable declared at the top level of the entire script.
set currentCount to 0
script Joe
global currentCount
on increment()
set currentCount to currentCount + 1
return currentCount
end increment
end script
tell Joe to increment() --result: 1
tell Joe to increment() --result: 2
The preceding example first sets the value of currentCount at the top level of the script. When AppleScript encounters the currentCount variable within the on increment handler, it first looks for an earlier occurrence within the handler, then at the top level of the script Joe . When AppleScript encounters the global declaration for currentCount at the top level of script object Joe , it continues looking at the top level of the script until it finds the original declaration for currentCount . This can't be done with a property of a script object, because AppleScript looks no further than the top level of a script object for that script object's properties.
Like the value of a script object's property, the value of a script object's global variable persists after the script object has been run, but not after the script itself has been run. Thus, telling Joe to increment repeatedly in the preceding example continues to increment the value of currentCount , but running the whole script again sets currentCount to 0 again before incrementing it.
The next example demonstrates how you can use a global variable declaration in a script object to associate a global variable with a property declared at the top level of a script.
property currentCount : 0
script Donna
property currentCount : 20
script Joe
global currentCount
on increment()
set currentCount to currentCount + 1
return currentCount
end increment
end script
tell Joe to increment()
end script
run Donna --result: 1
run Donna --result: 2
currentCount --result: 2
currentCount of Donna --result: 20
This script declares two separate currentCount properties: one at the top level of the script and one at the top level of the script object Donna . Because the script Joe declares the global variable currentCount , AppleScript looks for currentCount at the top level of the script, thus treating Joe's currentCount and currentCount at the top level of the script as the same variable.
If the script object Joe in the preceding example doesn't declare currentCount as a global variable, AppleScript treats Joe's currentCount and the currentCount at the top level of the script object Donna as the same variable. This leads to quite different results, as shown in the next example.
property currentCount : 0
script Donna
property currentCount : 20
script Joe
on increment()
set currentCount to currentCount + 1
return currentCount
end increment
end script
tell Joe to increment()
end script
run Donna --result: 21
run Donna --result: 22
currentCount --result: 0
currentCount of Donna --result:22
The scope of a variable declaration using the Set command at the top level of a script object is limited to the Run handler:
script Joe
set currentCount to 10
on increment()
global currentCount
set currentCount to currentCount + 2
end increment
return currentCount
end script
tell Joe to increment()
--error: "The variable currentCount is not defined."
run Joe--result: 10
In contrast to the way it treats such a declaration at the top level of a script, AppleScript treats the currentCount variable declared at the top level of the script object Joe in the preceding example as local to the script object's Run handler. Any subsequent attempt to use the same variable as a global results in an error.
Similarly, the scope of an explicit local variable declaration at the top level of a script object is limited to that script object's Run handler, even if the same identifier has been declared as a property at a higher level in the script:
property currentCount : 0
script Joe
local currentCount
set currentCount to 5
on increment()
set currentCount to currentCount + 1
end increment
end script
run Joe --result: 5
tell Joe to increment() --result: 1